package com.tensquare.mongo;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

/**
 * @Author: Mr.Liu
 * @Description: MongoDb入门小demo
 * @Date: Created in 16:37 2020/5/21
 */
public class MongoDemo {
    public static void main(String[] args) {
        MongoClient client = new MongoClient("192.168.242.128");  //创建连接
        MongoDatabase sx = client.getDatabase("sx");  //打开数据库
        MongoCollection<Document> spit = sx.getCollection("spit");  //获取集合
       /* BasicDBObject bson = new BasicDBObject("visits",new BasicDBObject("$gt",1000)); //构建条件*/
        FindIterable<Document> documents = spit.find(); //查询记录获取文档集合
        for (Document document : documents) {
           /* System.out.println(document.getString("content"));
            System.out.println(document.getString("_id"));
            System.out.println(document.getInteger("visits"));*/
            System.out.println(document);
        }
       /* Map<String, Object> map = new HashMap();
        map.put("content", "我要吐槽");
        map.put("userid", "9999");
        map.put("visits", 123);
        map.put("publishtime", new Date());
        Document document = new Document(map);
        spit.insertOne(document); //插入数据*/
        client.close();
    }
}
